package com.jinke.uang.common;

import android.app.Activity;
import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.Gson;
import com.jinke.uang.AppApplication;
import com.jinke.uang.cache.Acache;
import com.jinke.uang.cache.AcacheKeys;
import com.jinke.uang.constant.Constant;
import com.jinke.uang.bean.AuthData;

import java.util.Arrays;
import java.util.List;

/**
 * 用户认证路由的管理类
 */
public class UserAuthRouter {
    private static UserAuthRouter instance;
    private String currentStep = "identity_code";

    /**
     * 获取认证路由列表
     *
     * @return
     */
    public List<String> getRouterList() {
        String authData = Acache.get(AppApplication.getContext()).getAsString(AcacheKeys.AUTH);
        if (TextUtils.isEmpty(authData)) {
            return null;
        }
        Gson gson = new Gson();
        AuthData data = gson.fromJson(authData, AuthData.class);
        if (data.getAllCodes().size() < 7) {
            String[] arr = {"identity_code", "company_code", "borrowing_needs_code",
                    "personage_info_code", "contacts_code", "bank_info_code",
                    "additional_information_code"};
            return Arrays.asList(arr);
        }
        return data.getAllCodes();
    }

    /**
     * 获取未认证列表
     *
     * @return
     */
    public List<String> getNoAuthRouterList() {
        String authData = Acache.get(AppApplication.getContext()).getAsString(AcacheKeys.AUTH);
        if (TextUtils.isEmpty(authData)) {
            return null;
        }
        Gson gson = new Gson();
        AuthData data = gson.fromJson(authData, AuthData.class);
        if (data.getAllCodes().size() == 0) {
            String[] arr = {"identity_code", "company_code", "borrowing_needs_code",
                    "personage_info_code", "contacts_code", "bank_info_code",
                    "additional_information_code"};
            return Arrays.asList(arr);
        }
        return data.getNotCodes();
    }


    public static UserAuthRouter getInstance() {
        if (null == instance) {
            instance = new UserAuthRouter();
        }
        return instance;
    }


    public void jumpPage(String page) {
        switch (page) {
            case "identity_code":
                ARouter.getInstance().build(RouterMap.REAL_NAME_AUTH_PAGE).navigation();
                break;
            case "company_code":
                ARouter.getInstance().build(RouterMap.WORK_AUTH_PAGE).navigation();
                break;
            case "borrowing_needs_code":
                ARouter.getInstance().build(RouterMap.MONEY_AUTH_PAGE).navigation();
                break;
            case "personage_info_code":
                ARouter.getInstance().build(RouterMap.PERSONAL_AUTH_PAGE).navigation();
                break;
            case "contacts_code":
                ARouter.getInstance().build(RouterMap.CONTACT_AUTH_PAGE).navigation();
                break;
            case "bank_info_code":
                ARouter.getInstance().build(RouterMap.BANK_AUTH_PAGE).navigation();
                break;
            case "additional_information_code":
                ARouter.getInstance().build(RouterMap.PHOTO_AUTH_PAGE).navigation();
                break;
            default:
                break;
        }

    }


    public void jumpPageAndFinish(Activity activity, String page) {
        switch (page) {
            case "identity_code":
                ARouter.getInstance().build(RouterMap.REAL_NAME_AUTH_PAGE).navigation();
                activity.finish();
                break;
            case "company_code":
                ARouter.getInstance().build(RouterMap.WORK_AUTH_PAGE).navigation();
                activity.finish();
                break;
            case "borrowing_needs_code":
                ARouter.getInstance().build(RouterMap.MONEY_AUTH_PAGE).navigation();
                activity.finish();
                break;
            case "personage_info_code":
                ARouter.getInstance().build(RouterMap.PERSONAL_AUTH_PAGE).navigation();
                activity.finish();
                break;
            case "contacts_code":
                ARouter.getInstance().build(RouterMap.CONTACT_AUTH_PAGE).navigation();
                activity.finish();
                break;
            case "bank_info_code":
                ARouter.getInstance().build(RouterMap.BANK_AUTH_PAGE).navigation();
                activity.finish();
                break;
            case "additional_information_code":
                ARouter.getInstance().build(RouterMap.PHOTO_AUTH_PAGE).navigation();
                activity.finish();
                break;
            default:
                break;
        }

    }

    public void jumpNextPager(final Activity activity, String router) {

        if (isLastPager(router)) {
            Acache.get(AppApplication.getContext()).remove(AcacheKeys.AUTH);
            ARouter.getInstance().build(RouterMap.STATUS_DISPLAY).withInt(IntentKey
                    .STATUS_TYPE_KEY, Constant.STATUS_TYPE_AUTH).navigation();
            activity.finish();
            return;
        }
        switch (router) {
            case RouterMap.REAL_NAME_AUTH_PAGE:
                currentStep = "identity_code";
                break;
            case RouterMap.WORK_AUTH_PAGE:
                currentStep = "company_code";
                break;
            case RouterMap.MONEY_AUTH_PAGE:
                currentStep = "borrowing_needs_code";
                break;
            case RouterMap.PERSONAL_AUTH_PAGE:
                currentStep = "personage_info_code";
                break;
            case RouterMap.CONTACT_AUTH_PAGE:
                currentStep = "contacts_code";
                break;
            case RouterMap.BANK_AUTH_PAGE:
                currentStep = "bank_info_code";
                break;
            case RouterMap.PHOTO_AUTH_PAGE:
                currentStep = "additional_information_code";
                break;
            default:
                break;
        }

        jumpPageAndFinish(activity, getNoAuthRouterList().get(getNoAuthRouterList().indexOf
                (currentStep) + 1));
    }


    public String routerMap2Code(String router) {
        String code = "identity_code";
        switch (router) {
            case RouterMap.REAL_NAME_AUTH_PAGE:
                code = "identity_code";
                break;
            case RouterMap.WORK_AUTH_PAGE:
                code = "company_code";
                break;
            case RouterMap.MONEY_AUTH_PAGE:
                code = "borrowing_needs_code";
                break;
            case RouterMap.PERSONAL_AUTH_PAGE:
                code = "personage_info_code";
                break;
            case RouterMap.CONTACT_AUTH_PAGE:
                code = "contacts_code";
                break;
            case RouterMap.BANK_AUTH_PAGE:
                code = "bank_info_code";
                break;
            case RouterMap.PHOTO_AUTH_PAGE:
                code = "additional_information_code";
                break;
            default:
                break;
        }
        return code;
    }


    public void backRouter(String code) {
        switch (code) {
            case "identity_code":
                ARouter.getInstance().build(RouterMap.REAL_NAME_AUTH_PAGE).withBoolean(Constant
                        .IS_BACK, true).navigation();
                break;
            case "company_code":
                ARouter.getInstance().build(RouterMap.WORK_AUTH_PAGE).withBoolean(Constant
                        .IS_BACK, true).navigation();
                break;
            case "borrowing_needs_code":
                ARouter.getInstance().build(RouterMap.MONEY_AUTH_PAGE).withBoolean(Constant
                        .IS_BACK, true).navigation();
                break;
            case "personage_info_code":
                ARouter.getInstance().build(RouterMap.PERSONAL_AUTH_PAGE).withBoolean(Constant
                        .IS_BACK, true).navigation();
                break;
            case "contacts_code":
                ARouter.getInstance().build(RouterMap.CONTACT_AUTH_PAGE).withBoolean(Constant
                        .IS_BACK, true).navigation();
                break;
            case "bank_info_code":
                ARouter.getInstance().build(RouterMap.BANK_AUTH_PAGE).withBoolean(Constant
                        .IS_BACK, true).navigation();
                break;
            case "additional_information_code":
                ARouter.getInstance().build(RouterMap.PHOTO_AUTH_PAGE).withBoolean(Constant
                        .IS_BACK, true).navigation();
                break;
            default:
                break;
        }
    }

    public boolean isLastPager(String router) {
        if (getNoAuthRouterList().indexOf(routerMap2Code(router)) == getNoAuthRouterList().size()
                - 1) {
            return true;
        }
        return false;
    }


    public void jump2FirstNotAuthPager() {
        if (getNoAuthRouterList() != null && getNoAuthRouterList().size() >= 1) {
            jumpPage(getNoAuthRouterList().get(0));
        }
    }
}
